//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 12
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using ProxyHelpers.EWS;

namespace CreateAndAddAttachment
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential("username", "password", "domain");

            ItemIdType newId = EWSHelpers.CreateMessage(binding, "Item with file attachment");
            
            // Of course, this assumes that you have the jpeg image
            // in the path below...
            //
            AttachmentIdType attachmentId = CreateAndAddAttachment(
                binding,
                newId,
                @"c:\hi_mom_image.jpg");
            Console.WriteLine("Created attachment with id: " +
                attachmentId.Id);
        }

        /// <summary>
        /// Creates and adds an attachment to the parent item specified (Listing 12-10)
        /// </summary>
        /// <param name="binding">Binding to use for web method call</param>
        /// <param name="parentId">Id of parent item to attach to</param>
        /// <param name="fullFilePath">Full path (including file name) of 
        /// attachment</param>
        /// <returns>Id of attachment</returns>
        /// 
        public static AttachmentIdType CreateAndAddAttachment(
                                 ExchangeServiceBinding binding,
                                 ItemIdType parentId,
                                 string fullFilePath)
        {
            FileAttachmentType attachment = new FileAttachmentType();

            // Read the content of the file.  Remember the Content property is 
            // byte[], so no need to call System.Convert.ToBase64String().
            //
            attachment.Content = File.ReadAllBytes(fullFilePath);

            // Set the name to be the filename (minus the path)
            //
            attachment.Name = Path.GetFileName(fullFilePath);

            // Grab the content type
            //
            string contentType = EWSHelpers.GetMimeTypeForFile(fullFilePath);
            if (!String.IsNullOrEmpty(contentType))
            {
                attachment.ContentType = contentType;
            }

            // Make our request and parse the result
            //
            CreateAttachmentType request = new CreateAttachmentType();
            request.ParentItemId = parentId;
            request.Attachments = new AttachmentType[] { attachment };

            CreateAttachmentResponseType response =
                       binding.CreateAttachment(request);

            AttachmentInfoResponseMessageType responseMessage =
                    response.ResponseMessages.Items[0] as
                               AttachmentInfoResponseMessageType;

            if (responseMessage.ResponseCode == ResponseCodeType.NoError)
            {
                return responseMessage.Attachments[0].AttachmentId;
            }
            else
            {
                throw new Exception("Error trying to create attachment.  " +
                       "Response code: " + responseMessage.ResponseCode.ToString());
            }
        }
    }
}
